<template>
	<view class="page user-info">

		<nav-header title="" :show-right="false">
		</nav-header>

		<view class="content">
			<head-image :name="userInfo.nickName" :url="userInfo.headImageThumb" :size="100"
				@click="onShowFullImage()"></head-image>

			<view class="info-item">
				<view class="info-primary">
					<text class="info-username">
						{{ userInfo.userName }}
					</text>
					<view class="info-sex">
						<!-- <uni-icons v-show="userInfo.sex == 0" class="sex-boy" type="person-filled" size="20"
							color="darkblue"></uni-icons> -->
						<svg-icon v-show="userInfo.sex == 0" icon="sex" :width="32"></svg-icon>
						<!-- <uni-icons v-show="userInfo.sex == 1" class="sex-girl" type="person-filled" size="20"
							color="darkred"></uni-icons> -->
						<svg-icon v-show="userInfo.sex == 1" icon="woman" :width="32"></svg-icon>
					</view>
				</view>
				<text>
					昵称 ：{{ userInfo.nickName }}
				</text>
				<text>
					友号 ：{{ userInfo.userFriendId }} <text style="margin-left: 10rpx;"> 商号
						：{{ userInfo.userBusinessId }}</text>
				</text>
				<text v-if="userInfo.hometown">
					故乡 ：{{hometownReplace}}
				</text>
				<text v-if="userInfo.signature">
					个性签名 ：{{ userInfo.signature }}
				</text>
			</view>
		</view>
		<!-- <view class="line"></view> -->
		<view class="hello">
			<text>打招呼语</text>
			<input type="text" v-model="greetings" :placeholder="`您好, 我是${userStore.userInfo.nickName}`">
		</view>
		<view class="hello">
			<text>备注</text>
			<input type="text" v-model="friendNickName">
		</view>
		<view class="permission" v-if="showQuestion">
			<view class="hello">
				<text>问题：{{permission.question}}</text>
				<input type="text" v-model="answer" placeholder="请填写答案">
			</view>
		</view>
		<view class="line"></view>
		<view class="btn-group">
			<button class="btn btncolor" type="primary" v-if="!isFriend" @click.once="onAddFriend()">添加</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {},
				friendType: null,
				greetings: null,
				friendNickName: null,
				answer: null, //答案
				isBusinessFriend: null, //是否为友商
				permission: null, //加好友权限
				showQuestion: false //显示问题
			}
		},
		methods: {
			padNumber(number) {
				return number.toString().padStart(6, '0');
			},
			onShowFullImage() {
				let imageUrl = this.userInfo.headImage;
				if (imageUrl) {
					uni.previewImage({
						urls: [imageUrl]
					})
				}
			},
			onSendMessage() {
				this.chatStore.openChatRedirect('PRIVATE', this.userInfo.id)
				// let chat = {
				// 	type: 'PRIVATE',
				// 	targetId: this.userInfo.id,
				// 	showName: this.userInfo.nickName,
				// 	headImage: this.userInfo.headImage,
				// };
				// this.chatStore.openChat(chat);
				// let chatIdx = this.chatStore.findChatIdx(chat);
				// uni.navigateTo({
				// 	url: "/pageSubOne/chat/chat-box?chatIdx=" + chatIdx
				// })
			},
			onAddFriend() {
				this.$http({
					// url:`/friend/add?friendId=${this.userInfo.id}&friendType=${this.friendType}`,
					url: `/friend/applyAddFriend`,
					method: "POST",
					data: {
						friendId: this.userInfo.id,
						friendType: this.friendType,
						greetings: this.greetings,
						friendNickName: this.friendNickName,
						answer: this.answer,
					}
				}).then((data) => {
					if (this.permission.strategyType == 0) {
						this.friendStore.addFriend({
							id: this.userInfo.id,
							nickName: this.userInfo.nickName,
							headImage: this.userInfo.headImageThumb,
							answer: this.answer,
							online: this.userInfo.online,
						});
					}
					if(this.friendType==1){
						uni.showToast({
							title: '友商申请已发送',
							icon: 'none'
						})
					}else{
						uni.showToast({
							title: '好友申请已发送',
							icon: 'none'
						})
					}
					// 回退并刷新
					setTimeout(()=>{
						let pages = getCurrentPages();
						let prevPage = pages[pages.length - 2];
						prevPage.$vm.loadBusinessFriend(this.userInfo.id);
						uni.navigateBack();
					},1000)
				})
				/* if (this.permission.strategyType == 0) {
					this.$http({
						// url:`/friend/add?friendId=${this.userInfo.id}&friendType=${this.friendType}`,
						url: `/friend/applyAddFriend`,
						method: "POST",
						data: {
							friendId: this.userInfo.id,
							friendType: this.friendType,
							greetings: this.greetings,
							friendNickName: this.friendNickName
						}
					}).then((data) => {
						this.friendStore.addFriend({
							id: this.userInfo.id,
							nickName: this.userInfo.nickName,
							headImage: this.userInfo.headImageThumb,
							online: this.userInfo.online
						});
						uni.showToast({
							title: '对方已成为您的好友',
							icon: 'none'
						})
					})
					setTimeout(() => {
						// 回退并刷新
						let pages = getCurrentPages();
						let prevPage = pages[pages.length - 2];
						prevPage.$vm.loadBusinessFriend(this.userInfo.id);
						uni.navigateBack();
					}, 1000);
				} else if (this.permission.strategyType == 1) {
					this.$http({
						// url:`/friend/add?friendId=${this.userInfo.id}&friendType=${this.friendType}`,
						url: `/friend/applyAddFriend`,
						method: "POST",
						data: {
							friendId: this.userInfo.id,
							friendType: this.friendType,
							greetings: this.greetings,
							friendNickName: this.friendNickName
						}
					}).then((data) => {
						uni.showToast({
							title: '申请成功等待好友验证',
							icon: 'none'
						})
					})
					setTimeout(() => {
						uni.navigateBack();
					}, 1000)
				} */
			},
			updateFriendInfo() {
				// store的数据不能直接修改，深拷贝一份store的数据
				let friend = JSON.parse(JSON.stringify(this.friendInfo));
				friend.headImage = this.userInfo.headImageThumb;
				friend.nickName = this.userInfo.nickName;
				this.$http({
					url: "/friend/update",
					method: "PUT",
					data: friend
				}).then(() => {
					// 更新好友列表中的昵称和头像
					this.friendStore.updateFriend(friend);
					// 更新会话中的头像和昵称
					this.chatStore.updateChatFromFriend(this.userInfo);
				})
			},
			loadUserInfo(id) {
				this.$http({
					url: "/user/find/" + id,
					method: 'GET'
				}).then((user) => {
					user.userFriendId = this.padNumber(user.userFriendId);
					user.userBusinessId = this.padNumber(user.userBusinessId);
					this.userInfo = user;
					// 如果发现好友的头像和昵称改了，进行更新
					if (this.isFriend && (this.userInfo.headImageThumb != this.friendInfo.headImage ||
							this.userInfo.nickName != this.friendInfo.nickName)) {
						this.updateFriendInfo()
					}
					this.$http({
						url: "/friend/findAddFriendByUserId?userId=" + this.userInfo.id,
						method: "GET"
					}).then((data) => {
						this.permission = data
						console.log(data);
						if (this.permission && [2, 3].includes(this.permission.strategyType)) {
							this.showQuestion = true
						}
					})
				})
			},
			loadBusinessFriend(id) {
				this.$http({
					url: `/friend/checkFriendByFriendIdAndType?friendId=${id}&friendType=${this.friendType}`,
					method: "GET"
				}).then((data) => {
					this.isBusinessFriend = data.data
				})
			},
		},
		computed: {
			hometownReplace() {
				// const formattedAddress = this.userInfo.hometown.replace(/省|市|区|镇|街道|村|社区|委员会/g, '$&-');
				// return formattedAddress.replace(/-$/, '');
			
				return this.userInfo.hometown
					// ✅ 核心修复：使用更精准的负向断言
					.replace(
						/(?<!市辖区)(市|省|区|镇|街道|村|社区|委员会)(?!\s*(辖区|县|镇|乡))/g,
						'$&-' // 仅对非组合词添加连字符
					)
					.replace(/-$/, ''); // 移除末尾多余连字符
			},
			isFriend() {
				if (this.sourceType == 0 || this.sourceType == 2) {
					return !!this.friendInfo;
				} else {
					return this.isBusinessFriend
				}
			},
			friendInfo() {
				let friends = this.friendStore.friends;
				let friend = friends.find((f) => f.id == this.userInfo.id && f.friendType == 0);
				return friend;
			}
		},
		onLoad(options) {
			// 查询用户信息
			this.loadUserInfo(options.id);
			this.sourceType = options.sourceType
			this.friendType = options.friendType;
			if (this.sourceType == 1) {
				this.loadBusinessFriend(options.id)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.user-info {
		.content {
			height: 300rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 24rpx 30rpx;
			font-size: 27rpx;

			.info-item {
				display: flex;
				align-items: flex-start;
				flex-direction: column;
				padding-left: 40rpx;
				// padding-top: 40rpx;
				flex: 1;

				text {
					margin-top: 20rpx;
				}

				text:first-child {
					margin-top: 0;
				}


				.info-primary {
					display: flex;
					align-items: center;

					.info-username {
						font-size: 40rpx;
						font-weight: 600;
					}

					.info-sex {
						margin-left: 10rpx;
						margin-top: 10rpx;
					}
				}
			}
		}

		.hello {
			padding: 30rpx;
			display: flex;
			flex-direction: column;
			gap: 5px;

			input {
				border-radius: 5px;
				padding: 10px 10px;
				background-color: $gray-100;
			}
		}

		.line {
			margin: 30rpx;
			border-bottom: 1px solid #dddddd;
		}

		.btn-group {
			margin: 80rpx 30rpx;

			.btn {
				margin-top: 20rpx;
			}

			.btncolor {
				background-color: $brand-500;
				color: $white;
				font-size: 32rpx;
				border-radius: 50rpx;
			}

		}
	}
</style>